package com.yunfei.core.utils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.net.NetworkInterface;
import java.util.Enumeration;
@WebListener
public class SysInit
  implements ServletContextListener
{
  private String salt = "EFF3.0";
  
  public SysInit()
    throws Exception
  {
 /*   String license = ConfigService.getConfig("license");
    if (StringUtils.isEmpty(license))
    {
     // System.out.println("未注册");
      //JOptionPane.showMessageDialog(null, "请访问http://172.16.10.87获取注册码", "错误", 0);
     // System.exit(-1);
    }
    String code = getMacAddress() + this.salt;
    String encode = Md5Util.string2MD5(code);
    if (!Base64.encodeBase64String(encode.getBytes()).equals(license))
    {
      //System.out.println("注册码不正确");
      //JOptionPane.showMessageDialog(null, "注册码不正确,请访问http://172.16.10.87获取注册码", "错误", 0);
      //System.exit(-1);
    }*/
  }
  
  public SysInit(String arg) {}
  
  public void contextDestroyed(ServletContextEvent arg0) {}
  
  public void contextInitialized(ServletContextEvent arg0)
  {
    ServletContext application = arg0.getServletContext();
    
    String sysname = application.getInitParameter("sysname");
    application.setAttribute("sysname", sysname);
    application.setAttribute("ctx", application.getContextPath());
  }
  
  private String getMacAddress()
    throws Exception
  {
    Enumeration<NetworkInterface> ni = NetworkInterface.getNetworkInterfaces();
    while (ni.hasMoreElements())
    {
      NetworkInterface netI = (NetworkInterface)ni.nextElement();
      
      byte[] bytes = netI.getHardwareAddress();
      if ((netI.isUp()) && (netI != null) && (bytes != null) && (bytes.length == 6))
      {
        StringBuffer sb = new StringBuffer();
        for (byte b : bytes)
        {
          sb.append(Integer.toHexString((b & 0xF0) >> 4));
          
          sb.append(Integer.toHexString(b & 0xF));
          sb.append("-");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString().toUpperCase();
      }
    }
    return null;
  }
  
 /* public static void main(String[] args)
    throws Exception
  {
    SysInit s = new SysInit(null);
    String l = Md5Util.string2MD5(s.getMacAddress() + "EFF3.0");
    System.out.println(Base64.encodeBase64String(l.getBytes()));
  }*/
}
